package org.jaronsource.framework.plugins.security.validatecode;

import java.awt.Font;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

public class ImgFontByte {

	public Font getFont( int fontHeight ) {
		try {
			Font baseFont = Font.createFont( Font.TRUETYPE_FONT, new ByteArrayInputStream( hex2byte( getFontByteStr() ) ) );
			return baseFont.deriveFont( Font.PLAIN, fontHeight );
		}
		catch ( Exception e ) {
			return new Font( "Arial", Font.PLAIN, fontHeight );
		}
	}

	private byte[] hex2byte( String str ) {
		if ( str == null ) return null;
		str = str.trim();
		int len = str.length();
		if ( len == 0 || len % 2 == 1 ) return null;

		byte[] b = new byte[len / 2];
		try {
			for ( int i = 0; i < str.length(); i += 2 ) {
				b[i / 2] = (byte) Integer.decode( "0x" + str.substring( i, i + 2 ) ).intValue();
			}
			return b;
		}
		catch ( Exception e ) {
			return null;
		}
	}

	/**
	 * ttf字体文件的十六进制字符串
	 * 
	 * @return
	 */
	private String getFontByteStr() {

		InputStream is = ImgFontByte.class.getResourceAsStream( "/secode.ttf" );
		java.io.BufferedInputStream fb = new java.io.BufferedInputStream( is );
		String res = "";
		try {
			byte[] b = new byte[1024];

			if ( is == null ) { return ""; }

			int bytesRead = 0;
			while ( true ) {
				bytesRead = is.read( b, 0, 1024 ); // return final read bytes counts
				if ( bytesRead == -1 ) {// end of InputStream
					return res;
				}
				res += new String( b, 0, bytesRead ); // convert to string using bytes
			}

		}
		catch ( Exception e ) {
			e.printStackTrace();
			return "";
		}

	}

}